<template>
  <div
    class="image-clip-path-container"
    ref="elementRef"
    :style="{ top: state.top, opacity: state.opacity }"
  >
    <div class="image-clip-path-title">
      裁切
      <Icon class="image-clip-path-icon" type="md-close" size="20" @click="handleClose" />
    </div>
    <ul class="image-box-tab">
      <li
        v-for="item in state.tabs"
        :key="item.name"
        :class="item.name === state.active && 'active'"
        @click="handleClickTab(item.name)"
      >
        {{ item.label }}
      </li>
      <div class="block" :style="{ left: state.left }"></div>
    </ul>
    <div class="line"></div>
    <ul class="image-clip-path-base" v-if="state.active === 'base'">
      <li v-for="item in options" :key="item.value" @click="addClipPath(item.value)">
        {{ item.label }}
      </li>
    </ul>
    <ul class="image-clip-path-custom" v-if="state.active === 'custom'">
      <li v-for="item in options" :key="item.value" @click="addCustomClipPath(item.value)">
        {{ item.label }}
      </li>
    </ul>
  </div>
</template>

<script name="imagebox" setup lang="ts">
import { ref, watch } from 'vue';
import useSelect from '@/hooks/select';
import { useI18n } from 'vue-i18n';

const { canvasEditor, isOne } = useSelect();
const update = getCurrentInstance();

const elementRef = ref(null);

const { t } = useI18n();
const type = ref('');
const options = [
  {
    label: t('polygonClip'),
    value: 'polygon',
  },
  {
    label: t('rectClip'),
    value: 'rect',
  },
  {
    label: t('circleClip'),
    value: 'circle',
  },
  {
    label: t('triangleClip'),
    value: 'triangle',
  },
  {
    label: t('polygonClipInverted'),
    value: 'polygon-inverted',
  },
  {
    label: t('rectClipInverted'),
    value: 'rect-inverted',
  },
  {
    label: t('circleClipInverted'),
    value: 'circle-inverted',
  },
  {
    label: t('triangleClipInverted'),
    value: 'triangle-inverted',
  },
];

const state = reactive({
  top: '100%',
  opacity: 0,

  left: '5px',
  active: 'base',
  tabs: [
    {
      name: 'base',
      label: '基础图形',
    },
    {
      name: 'custom',
      label: '自定义图形',
    },
  ],
});

const props = defineProps(['visible']);
const emit = defineEmits(['handle-close']);

const handleClickTab = (name) => {
  state.active = name;

  if (name === state.tabs[0].name) {
    state.left = '5px';
  } else if (name === state.tabs[1].name) {
    state.left = '79px';
  }
};

const handleClose = () => {
  emit('handle-close');
};

const addClipPath = async (name) => {
  canvasEditor.addClipPathToImage(name);
};
const removeClip = () => {
  canvasEditor.removeClip();
};

const addCustomClipPath = () => {
  canvasEditor.addImageClipToImage('');
};

console.log(removeClip, isOne);
const init = () => {
  const activeObject = canvasEditor.canvas.getActiveObjects()[0];
  if (activeObject) {
    type.value = activeObject.type;
    update?.proxy?.$forceUpdate();
  }
};

watch(
  () => props.visible,
  (visible) => {
    console.log(visible);
    if (visible) {
      state.top = 0;
      state.opacity = 1;
    } else {
      state.top = '100%';
      state.opacity = 0;
    }
  }
);

onMounted(() => {
  canvasEditor.on('selectOne', init);
});

onBeforeUnmount(() => {
  canvasEditor.off('selectOne', init);
});
</script>

<style lang="less" scoped>
.image-clip-path-container {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 100;
  width: 100%;
  height: 100%;
  background-color: var(--background-white-color);
  overflow: hidden;
  transition: all 0.3s ease-in-out;
  padding: 8px 10px;
  border-radius: 8px;

  .image-clip-path-title {
    width: 100%;
    font-size: 12px;
    color: #999;
    margin: 6px 0;
    padding: 0 6px;
    display: flex;
    justify-content: space-between;

    .image-clip-path-icon {
      color: var(--primary-color);
      cursor: pointer;

      &:hover {
        color: var(--primary-color-hover);
      }
    }
  }

  .image-box-tab {
    width: 160px;
    height: 40px;
    border-radius: 6px;
    background-color: var(--background-primary-color);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-left: 70px;
    position: relative;

    li {
      width: 75px;
      height: 28px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 12px;
      color: #999;
      cursor: pointer;
      position: relative;
      z-index: 2;

      &.active {
        color: var(--primary-color);
        font-weight: bold;
      }
    }

    .block {
      width: 75px;
      height: 28px;
      border-radius: 6px;
      background-color: var(--color--bg-float);
      box-shadow: var(--shadow-m);
      position: absolute;
      content: ' ';
      top: 5px;
      z-index: 1;
      transition: left 0.3s ease-in-out;
    }
  }
}
</style>
